package com.dubbo.consumer.entity.response;

import com.dubbo.consumer.entity.constant.MessageConstant;
import com.dubbo.consumer.entity.constant.ResponseCodeConstant;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @Author Gavin
 * @Date 2023/01/13 11:44
 */
public class Response extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = -4958893316866214094L;

    public static Response result(int result) {
        return result > 0 ? Response.success() : Response.failure();
    }

    public static Response result(boolean result) {
        return result ? Response.success() : Response.failure();
    }

    public static Response success() {
        Response response = new Response();
        response.put("code", ResponseCodeConstant.SUCCESS);
        response.put("message", MessageConstant.SUCCESS);
        response.put("data", null);
        return response;
    }

    public static Response failure() {
        Response response = new Response();
        response.put("code", ResponseCodeConstant.FAILURE);
        response.put("message", MessageConstant.FAILURE);
        response.put("data", null);
        return response;
    }

    public static Response failure(int code, String message) {
        Response response = new Response();
        response.put("code", code);
        response.put("message", message);
        response.put("data", null);
        return response;
    }

    public static Response data(Object data) {
        Response response = new Response();
        response.put("code", ResponseCodeConstant.SUCCESS);
        response.put("message", null);
        response.put("data", data);
        return response;
    }
}
